package com.luomu.base.api.cookBook.excel.utils;


import com.luomu.base.api.exception.ZTBusinessException;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class BigDecimalUtil {

    public static String bigDecimalToStr(BigDecimal bigDecimal, String pattern) {
        DecimalFormat df = new DecimalFormat(pattern);
        if (bigDecimal == null) {
            return "0.00";
        }
        if (bigDecimal.compareTo(BigDecimal.ZERO) == 0) {
            return "0.00";
        }
        return df.format(bigDecimal);
    }

    /**
     * 后台金额(单位毫厘)传到前台元，如单品价格 再转成字符串
     * @param amount
     * @return java.math.BigDecimal
     * @see BigDecimal
     */
    public static String Long2BigDecimalStr(Long amount) {

        if (amount != null) {
            try {
                BigDecimal bd = new BigDecimal("10000");
                BigDecimal bAmount = new BigDecimal(amount);
                BigDecimal divide = bAmount.divide(bd, 2, BigDecimal.ROUND_UP);
                DecimalFormat df = new DecimalFormat(",##0.00");
                return df.format(divide);
            }
            catch (Exception e) {
                throw new ZTBusinessException("Long2BigDecimal数据转换错误: " + e);
            }
        }
        return null;
    }

    public static BigDecimal Long2BigDecimal(Long amount) {

        if (amount != null) {
            try {
                BigDecimal bd = new BigDecimal("10000");
                BigDecimal bAmount = new BigDecimal(amount);
                return bAmount.divide(bd, 2, BigDecimal.ROUND_UP);
            }
            catch (Exception e) {
                throw new ZTBusinessException("Long2BigDecimal数据转换错误: " + e);
            }
        }
        return null;
    }
    /**
     * @return java.math.BigDecimal
     * @see BigDecimal
     */
    public static String toBigDecimalStr(BigDecimal amount) {
        if (amount != null) {
            try {
                DecimalFormat df = new DecimalFormat(",##0.00");
                return df.format(amount);
            }
            catch (Exception e) {
                throw new ZTBusinessException("Long2BigDecimal数据转换错误: " + e);
            }
        }
        return null;
    }


    public static void main(String[] args) {
        System.out.println(Long2BigDecimalStr(20000L));
        System.out.println(Long2BigDecimalStr(2001L));
        System.out.println(Long2BigDecimalStr(201L));
        System.out.println(Long2BigDecimalStr(211L));
        System.out.println(Long2BigDecimalStr(21L));
        System.out.println(Long2BigDecimalStr(211232423141341234L));
    }

}
